<template>
  <div class="ele-body">
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>设备管理</span>
      </div>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="url"
        :max-height="defaultHeight"
        :parse-data="parseData"
        :selection.sync="selection"
        :where="where"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-form
            :model="where"
            class="ele-form-search"
            label-width="75px"
            @keyup.enter.native="reload"
            @submit.native.prevent
          >
            <div class="app-container">
              <div class="filter-container">
                <el-select
                  v-model="where.userStat"
                  class="filter-item"
                  clearable
                  placeholder="设备状态"
                  style="width: 200px"
                  @change="reload"
                  @clear="reload"
                >
                  <el-option
                    v-for="item in userStatList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>

                <el-input
                  v-model="where.name"
                  class="filter-item"
                  clearable
                  placeholder="设备名称"
                  style="width: 220px"
                  @clear="reload"
                />
                <el-button
                  class="filter-item"
                  icon="el-icon-search"
                  type="primary"
                  @click="reload"
                  >搜索
                </el-button>

                <el-button
                  class="filter-item"
                  icon="el-icon-refresh"
                  style="margin-left: 2px"
                  @click="reset"
                  >重置
                </el-button>
              </div>
            </div>
          </el-form>
        </template>

        <!-- 推广行业列 -->
        <template slot="datetime" slot-scope="{ row }">
          <span v-if="row.datetime">{{ row.datetime }}</span>
          <span v-if="!row.datetime">-</span>
        </template>
        <!-- 推广状态列 -->
        <template slot="status" slot-scope="{ row }">
          <ele-dot
            v-if="row.status == 1"
            type="success"
            :ripple="ripple"
            text="设备正常"
          />
          <ele-dot
            v-if="row.status != 1"
            type="danger"
            :ripple="ripple"
            text="设备状态异常"
          />
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link
            v-if="$hasPermission('xhsdevice:update')"
            :underline="false"
            icon="el-icon-edit-outline"
            type="primary"
            @click="openEdit(row)"
            >修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此用户吗？"
            @confirm="remove(row)"
          >
            <el-link
              v-if="$hasPermission('xhsdevice:delete')"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              type="danger"
              >删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>

    <!-- 编辑弹窗 -->
    <vue-edit :data="current" :visible.sync="showEdit" @done="reload" />
  </div>
</template>

<script>
import VueEdit from "./vue-edit";

export default {
  name: "xhsDevice",
  components: { VueEdit },
  data() {
    return {
      // 闪光点
      ripple: true,
      // 表格高度
      defaultHeight: 0,
      // 推广账户状态
      userStatList: [
        { value: 1, label: "设备正常" },
        { value: 2, label: "设备状态异常" },
      ],
      // 表格数据接口
      url: "/Xhsdevice/read",
      // 表格列配置
      columns: [
        {
          columnKey: "index",
          type: "index",
          width: 80,
          label: "序号",
          align: "center",
          showOverflowTooltip: true,
        },
        {
          prop: "name",
          label: "设备名称",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "deviceId",
          label: "设备ID",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "create",
          label: "创建时间",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "datetime",
          slot: "datetime",
          label: "最后通信时间",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "remark",
          label: "设备备注",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          prop: "status",
          slot: "status",
          label: "设备状态",
          sortable: "custom",
          showOverflowTooltip: true,
        },
        {
          columnKey: "action",
          label: "操作",
          width: 130,
          resizable: false,
          slot: "action",
        },
      ],
      // 表格搜索条件
      where: {},
      // 是否显示编辑弹窗
      showEdit: false,
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
  },

  methods: {
    /*设置表格高度*/
    getHeight() {
      this.defaultHeight = window.innerHeight - 360;
    },

    /*表格数据加载*/
    parseData(res) {
      return {
        code: res.code,
        data: res.data,
        count: res.count,
        msg: res.msg,
      };
    },

    /* 刷新表格 */
    reload(isPage) {
      let obj = { where: this.where };
      if (!isPage) {
        obj.page = 1;
      }
      this.$refs.table.reload(obj);
    },

    /* 重置搜索 */
    reset() {
      for (let key in this.where) {
        this.where[key] = null;
      }
      this.reload();
    },

    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .delete("/Xhsdevice/delete/id/" + row.id)
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
  },
};
</script>

<style scoped>
.ele-table-tool .is-disabled {
  cursor: not-allowed;
  filter: Alpha(opacity=0.65);
  -moz-opacity: 0.65;
  opacity: 0.65;
}
</style>
